package p270jp.pxv.android.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ViewDataBinding;
import com.google.android.exoplayer2.source.hls.playlist.DefaultHlsPlaylistTracker;
import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistTracker;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import java.util.Objects;
import p005a5.C0022b;
import p014ae.View$OnClickListenerC0163u;
import p046bf.PixivAppUserAgents;
import p064c6.Preconditions;
import p097d7.DefaultLoadControl;
import p097d7.DefaultRenderersFactory;
import p097d7.ExoPlayerFactory;
import p097d7.SimpleExoPlayer;
import p123e7.AnalyticsCollector;
import p189gn.AbstractC3435a;
import p253j8.BandwidthMeter;
import p253j8.DefaultBandwidthMeter;
import p253j8.DefaultDataSourceFactory;
import p253j8.DefaultHttpDataSourceFactory;
import p253j8.LoadErrorHandlingPolicy;
import p270jp.pxv.android.R;
import p270jp.pxv.android.model.pixiv_sketch.LiveHlsMediaPlayer;
import p279k8.C4507u;
import p314lg.ViewRenewalLiveBinding;
import p325m0.MutabilityOwnership;
import p374nm.C5400k0;
import p374nm.C5402l0;
import p376no.Koin;
import p404oo.AbstractC5475a;
import p563v7.HlsDataSourceFactory;
import p563v7.HlsExtractorFactory;
import p563v7.HlsMediaSource;
import p588w7.HlsPlaylistParserFactory;
import p603wm.AbstractC6949c;
import p603wm.Unit;
import p665z6.C7467v;
import p671zc.CompositeDisposable;
import p677zi.PixivImageLoader;

/* compiled from: RenewalLiveView.kt */
/* renamed from: jp.pxv.android.view.RenewalLiveView */
/* loaded from: classes2.dex */
public final class RenewalLiveView extends FrameLayout implements AbstractC5475a {

    /* renamed from: o */
    public static final /* synthetic */ int f19659o;

    /* renamed from: a */
    public LiveHlsMediaPlayer f19660a;

    /* renamed from: b */
    public ViewRenewalLiveBinding f19661b;

    /* renamed from: c */
    public final ObservableBoolean f19662c;

    /* renamed from: d */
    public final ObservableBoolean f19663d;

    /* renamed from: e */
    public final ObservableBoolean f19664e;

    /* renamed from: f */
    public AbstractC3435a<Unit> f19665f;

    /* renamed from: g */
    public AbstractC3435a<Unit> f19666g;

    /* renamed from: h */
    public float f19667h;

    /* renamed from: i */
    public boolean f19668i;

    /* renamed from: j */
    public String f19669j;

    /* renamed from: k */
    public final CompositeDisposable f19670k;

    /* renamed from: l */
    public final AbstractC6949c f19671l;

    /* renamed from: m */
    public final AbstractC6949c f19672m;

    /* renamed from: n */
    public final p374nm.RenewalLiveView f19673n;

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public RenewalLiveView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0, 4);
        MutabilityOwnership.m3745m(context, "context");
    }

    public RenewalLiveView(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, (i2 & 2) != 0 ? null : attributeSet, (i2 & 4) != 0 ? 0 : i);
        ObservableBoolean observableBoolean = new ObservableBoolean(true);
        this.f19662c = observableBoolean;
        ObservableBoolean observableBoolean2 = new ObservableBoolean(false);
        this.f19663d = observableBoolean2;
        ObservableBoolean observableBoolean3 = new ObservableBoolean(false);
        this.f19664e = observableBoolean3;
        this.f19670k = new CompositeDisposable();
        this.f19671l = Preconditions.m9802P(1, new C5400k0(this, null, null));
        this.f19672m = Preconditions.m9802P(1, new C5402l0(this, null, null));
        this.f19673n = new p374nm.RenewalLiveView(this);
        if (!isInEditMode()) {
            ViewDataBinding c = DataBindingUtil.m11370c(LayoutInflater.from(context), R.layout.view_renewal_live, this, true);
            MutabilityOwnership.m3746l(c, "inflate(LayoutInflater.f…renewal_live, this, true)");
            ViewRenewalLiveBinding fbVar = (ViewRenewalLiveBinding) c;
            this.f19661b = fbVar;
            fbVar.mo3801z(observableBoolean);
            ViewRenewalLiveBinding fbVar2 = this.f19661b;
            if (fbVar2 != null) {
                fbVar2.mo3803A(observableBoolean3);
                ViewRenewalLiveBinding fbVar3 = this.f19661b;
                if (fbVar3 != null) {
                    fbVar3.mo3802C(observableBoolean2);
                    ViewRenewalLiveBinding fbVar4 = this.f19661b;
                    if (fbVar4 != null) {
                        fbVar4.f21590q.setOnClickListener(new View$OnClickListenerC0163u(this, 24));
                    } else {
                        MutabilityOwnership.m3737u("binding");
                        throw null;
                    }
                } else {
                    MutabilityOwnership.m3737u("binding");
                    throw null;
                }
            } else {
                MutabilityOwnership.m3737u("binding");
                throw null;
            }
        }
    }

    private final PixivAppUserAgents getPixivAppUserAgents() {
        return (PixivAppUserAgents) this.f19672m.getValue();
    }

    private final PixivImageLoader getPixivImageLoader() {
        return (PixivImageLoader) this.f19671l.getValue();
    }

    private final void setupPlayer(String str) {
        BandwidthMeter cVar;
        DefaultBandwidthMeter kVar = new DefaultBandwidthMeter();
        Context context = getContext();
        DefaultTrackSelector defaultTrackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.C2011a(kVar));
        DefaultRenderersFactory gVar = new DefaultRenderersFactory(context);
        DefaultLoadControl eVar = new DefaultLoadControl();
        int i = C4507u.f20251a;
        Looper myLooper = Looper.myLooper();
        if (myLooper == null) {
            myLooper = Looper.getMainLooper();
        }
        AnalyticsCollector.C2890a aVar = new AnalyticsCollector.C2890a();
        synchronized (ExoPlayerFactory.class) {
            if (ExoPlayerFactory.f11830a == null) {
                ExoPlayerFactory.f11830a = new DefaultBandwidthMeter.C3939b().m4903a();
            }
            cVar = ExoPlayerFactory.f11830a;
        }
        SimpleExoPlayer zVar = new SimpleExoPlayer(context, gVar, defaultTrackSelector, eVar, null, cVar, aVar, myLooper);
        HlsMediaSource.C6821b bVar = new HlsMediaSource.C6821b(new DefaultDataSourceFactory(getContext(), kVar, new DefaultHttpDataSourceFactory(getPixivAppUserAgents().f5004a, kVar)));
        ViewRenewalLiveBinding fbVar = this.f19661b;
        if (fbVar != null) {
            fbVar.f21591r.setPlayer(null);
            ViewRenewalLiveBinding fbVar2 = this.f19661b;
            if (fbVar2 != null) {
                fbVar2.f21591r.setPlayer(zVar);
                Uri parse = Uri.parse(str);
                Handler handler = new Handler();
                p374nm.RenewalLiveView j0Var = this.f19673n;
                HlsDataSourceFactory eVar2 = bVar.f28955a;
                HlsExtractorFactory fVar = bVar.f28956b;
                C0022b bVar2 = bVar.f28959e;
                LoadErrorHandlingPolicy rVar = bVar.f28960f;
                HlsPlaylistTracker.AbstractC2000a aVar2 = bVar.f28958d;
                HlsPlaylistParserFactory cVar2 = bVar.f28957c;
                Objects.requireNonNull((C7467v) aVar2);
                HlsMediaSource iVar = new HlsMediaSource(parse, eVar2, fVar, bVar2, rVar, new DefaultHlsPlaylistTracker(eVar2, rVar, cVar2), false, null, null);
                if (j0Var != null) {
                    iVar.m2502g(handler, j0Var);
                }
                LiveHlsMediaPlayer liveHlsMediaPlayer = new LiveHlsMediaPlayer(zVar);
                this.f19660a = liveHlsMediaPlayer;
                liveHlsMediaPlayer.setHlsMediaSource(iVar);
                this.f19669j = str;
                return;
            }
            MutabilityOwnership.m3737u("binding");
            throw null;
        }
        MutabilityOwnership.m3737u("binding");
        throw null;
    }

    /* renamed from: a */
    public final void m4472a(String str) {
        if (!MutabilityOwnership.m3749h(this.f19669j, str)) {
            m4471b();
        }
        if (!this.f19668i) {
            if (this.f19660a == null) {
                setupPlayer(str);
            }
            LiveHlsMediaPlayer liveHlsMediaPlayer = this.f19660a;
            if (liveHlsMediaPlayer != null) {
                liveHlsMediaPlayer.playback();
            }
            this.f19668i = true;
        }
    }

    /* renamed from: b */
    public final void m4471b() {
        LiveHlsMediaPlayer liveHlsMediaPlayer = this.f19660a;
        if (liveHlsMediaPlayer != null) {
            liveHlsMediaPlayer.stop();
        }
        this.f19668i = false;
        LiveHlsMediaPlayer liveHlsMediaPlayer2 = this.f19660a;
        if (liveHlsMediaPlayer2 != null) {
            liveHlsMediaPlayer2.removePlayerEventListener();
        }
        LiveHlsMediaPlayer liveHlsMediaPlayer3 = this.f19660a;
        if (liveHlsMediaPlayer3 != null) {
            liveHlsMediaPlayer3.release();
        }
        this.f19660a = null;
        this.f19669j = null;
    }

    @Override // android.view.View, android.view.ViewGroup
    public void dispatchDraw(Canvas canvas) {
        MutabilityOwnership.m3745m(canvas, "canvas");
        if (this.f19667h > 0.0f) {
            int save = canvas.save();
            Path path = new Path();
            RectF rectF = new RectF(0.0f, 0.0f, (float) canvas.getWidth(), (float) canvas.getHeight());
            float f = this.f19667h;
            path.addRoundRect(rectF, f, f, Path.Direction.CW);
            canvas.clipPath(path);
            super.dispatchDraw(canvas);
            canvas.restoreToCount(save);
            return;
        }
        super.dispatchDraw(canvas);
    }

    @Override // p404oo.AbstractC5475a
    public Koin getKoin() {
        return AbstractC5475a.C5476a.m3211a(this);
    }

    public final AbstractC3435a<Unit> getOnLoadError() {
        return this.f19666g;
    }

    public final AbstractC3435a<Unit> getRefreshListener() {
        return this.f19665f;
    }

    @Override // android.view.View, android.view.ViewGroup
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.f19670k.m80f();
        m4471b();
    }

    public final void setCornerRadius(float f) {
        this.f19667h = f;
        invalidate();
    }

    public final void setLoading(boolean z) {
        ObservableBoolean observableBoolean = this.f19662c;
        if (z != observableBoolean.f2657b) {
            observableBoolean.f2657b = z;
            observableBoolean.m11381c();
        }
    }

    public final void setMuted(boolean z) {
        ObservableBoolean observableBoolean = this.f19664e;
        if (z != observableBoolean.f2657b) {
            observableBoolean.f2657b = z;
            observableBoolean.m11381c();
        }
    }

    public final void setNeedRefresh(boolean z) {
        ObservableBoolean observableBoolean = this.f19663d;
        if (z != observableBoolean.f2657b) {
            observableBoolean.f2657b = z;
            observableBoolean.m11381c();
        }
    }

    public final void setOnLoadError(AbstractC3435a<Unit> aVar) {
        this.f19666g = aVar;
    }

    public final void setRefreshListener(AbstractC3435a<Unit> aVar) {
        this.f19665f = aVar;
    }

    public final void setThumbnailImageURL(String str) {
        if (str == null) {
            ViewRenewalLiveBinding fbVar = this.f19661b;
            if (fbVar != null) {
                fbVar.f21592s.setVisibility(8);
            } else {
                MutabilityOwnership.m3737u("binding");
                throw null;
            }
        } else {
            ViewRenewalLiveBinding fbVar2 = this.f19661b;
            if (fbVar2 != null) {
                fbVar2.f21592s.setVisibility(0);
                PixivImageLoader pixivImageLoader = getPixivImageLoader();
                Context context = getContext();
                MutabilityOwnership.m3746l(context, "context");
                ViewRenewalLiveBinding fbVar3 = this.f19661b;
                if (fbVar3 != null) {
                    ImageView imageView = fbVar3.f21592s;
                    MutabilityOwnership.m3746l(imageView, "binding.thumbnailImageView");
                    pixivImageLoader.m59g(context, str, imageView);
                    return;
                }
                MutabilityOwnership.m3737u("binding");
                throw null;
            }
            MutabilityOwnership.m3737u("binding");
            throw null;
        }
    }
}
